package com.example.innospace.demos.web.controller;

import com.example.innospace.demos.pojo.entity.Teacher;
import com.example.innospace.demos.pojo.enums.MessageEnum;
import com.example.innospace.demos.pojo.enums.ResponseCodeEnum;
import com.example.innospace.demos.pojo.result.R;
import com.example.innospace.demos.pojo.vo.TeacherVO;
import com.example.innospace.demos.pojo.vo.TeacherWithToken;
import com.example.innospace.demos.utils.JWTUtils;
import com.example.innospace.demos.utils.OssUtil;
import com.example.innospace.demos.web.service.TeacherService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 老师端
 * TODO 头像部分没写
 */
@RestController
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    private OssUtil ossUtil;
    @Autowired
    private TeacherService teacherService;

    /**
     * 老师用户登录
     * @param
     * @return
     */
    @PostMapping("/login")
    public R login(HttpServletRequest request, @RequestBody Teacher teacher)
    {

        Teacher teacher1=teacherService.findById(teacher.getTeacherId());
        //查询用户
        if(teacher1==null)
        {
            return R.error(ResponseCodeEnum.BAD_REQUEST,MessageEnum.USER_NOT_EXIST);
        }
        //查询到了
        //判断密码是否正确
        //loginUser对象中的password是密文
        if(teacher1.getPassword().equals(teacher.getPassword())) {
            //登录成功
            request.getSession().setAttribute("teacherId", teacher.getTeacherId());
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", teacher.getTeacherId());
            claims.put("username", teacher.getName());
            //登录成功以后，从登录用户中获得id，获得用户名字，通过JwtUtil来获得这样一个（用户）字段下的Token令牌
            String token = JWTUtils.genToken(claims);
            TeacherWithToken teacherWithToken=new TeacherWithToken();
            Teacher teacher2=teacherService.findById(teacher.getTeacherId());
            teacherWithToken.setTeacher(teacher2);
            teacherWithToken.setToken(token);
            return R.success(ResponseCodeEnum.SUCCESS,teacherWithToken,MessageEnum.LOGIN_SUCCESS);//将令牌传出
        }
        return R.error(ResponseCodeEnum.BAD_REQUEST,MessageEnum.PASSWORD_ERROR);
    }


    /**
     * 退出登录
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        //1.清除session中的用户id
        request.getSession().removeAttribute("teacherId");
        //2.返回结果
        return R.success(ResponseCodeEnum.SUCCESS, MessageEnum.LOGOUT_SUCCESS);
        //3.页面会跳转回登录界面

    }

    /**
     *  // 修改老师密码接口
     * @param teacherId,
     * @param oldPassword
     * @param newPassword
     * @return
     */
    @PutMapping("/teacher/password")
    public R updatePassword(@RequestBody String  teacherId,
                            @RequestBody String oldPassword,
                            @RequestBody String newPassword) {
        String password=teacherService.getpassword(teacherId);
        if(password.equals(oldPassword))
        {
            teacherService.updatePassword(newPassword,teacherId);
            return R.success(ResponseCodeEnum.SUCCESS,MessageEnum.OPERATION_SUCCESS);
        }
        return R.error(ResponseCodeEnum.SUCCESS,MessageEnum.PASSWORD_ERROR);
    }


    /**
     * 教师信息回显
     * @param teacherId
     * @return
     */
    @GetMapping("/teacher/info")
    public R<TeacherVO> findById(@RequestParam String teacherId)
    {
        Teacher teacher=teacherService.findById(teacherId);
        TeacherVO teacherVO=new TeacherVO();
        BeanUtils.copyProperties(teacher,teacherVO);
        return R.success(ResponseCodeEnum.SUCCESS,teacherVO,MessageEnum.OPERATION_SUCCESS);
    }


    /**
     * 修改老师个人信息
     * @param teacher
     * @return
     */
    @PutMapping
    public R update(@RequestBody Teacher teacher)
    {
        teacherService.updateTeacherInfo(teacher.getTeacherId(),teacher);
        return R.success(ResponseCodeEnum.SUCCESS,MessageEnum.OPERATION_SUCCESS);
    }


    /**
     * 上传头像
     * @param teacherId
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/teacher/uploadAvatar")
    public R<String> upload(@RequestParam String teacherId,@RequestParam("file") MultipartFile file) throws IOException {
        String fileUrl = ossUtil.uploadFile(file);

        // 将文件 URL 存储到数据库（这里需要调用 Service 层）
        teacherService.updateAvatar(teacherId, fileUrl);

        return R.success(ResponseCodeEnum.SUCCESS,fileUrl,MessageEnum.OPERATION_SUCCESS);
    }


    /**
     * 下载头像
     * @param teacherId
     * @return
     */
    @GetMapping("/teacher/getAvatar")
    public R<String> getAvatar(@RequestParam String teacherId) {
        // 从数据库获取头像文件的 URL（这里需要调用 Service 层）
        String fileUrl = teacherService.getAvatar(teacherId);

        // 返回文件的 URL
        return R.success(ResponseCodeEnum.SUCCESS,fileUrl,MessageEnum.OPERATION_SUCCESS);
    }



}
